//登录
var $loginBtn = $(".login-btn"),
    $loginBox = $(".login-box");
/*呼出菜单*/
$loginBtn.on('touchstart', function () {
    $(".overlay").show();
    $loginBox.addClass('bounceIn');
})
$("#cancel").on('click', function () {
    $(".overlay").hide();
    $loginBox.removeClass('bounceIn');

})

var $login = $("#login"),
    $phone = $("#user-phone"),
    $capcha = $("#checkcode");
$login.on('touchstart', function () {
    var userPhone = $phone.val(),
        capcha = $capcha.val();
    if (bingUtil.required($phone, '手机号', /^1[356789]\d{9}$/) && bingUtil.required($capcha, '验证码', null)) {
        //验证成功
        dialog.loading.open('正在登录...');
        bingUtil.myAjax({
            url: '/',
            type: 'post',
            data: { "phonenum": userPhone, "capcha": capcha },
            callback: function (res) {
                dialog.loading.close();
                /*----------登录成功操作--------*/
                console.log('登录成功');

            }
        })

    }
});
/*********用户协议*****/
$(".open-agreement").on('click', function () {
    $(".agreement").addClass('bounceIn');
});
$("#disagree").on('click', function () {
    $(".overlay").hide();
    $(".agreement").removeClass('bounceIn');
    $loginBox.removeClass('bounceIn');
});
$("#agree").on('click', function () {
    $(".agreement").removeClass('bounceIn');
});

/***获取验证码****/
var $getCapcha = $("#capcha-btn");
var duration = 30,//验证码延迟时间
    timer = null;
$getCapcha.on('click', function () {
    if (bingUtil.required($phone, '手机号', /^1[356789]\d{9}$/)) {
        var userPhone = $phone.val();
        var self = $(this); timer = null;
        self.attr('disabled', true);
        SetRemainTime();
        $.ajax({
            url: '',
            type: 'get',
            data: { "userPhone": userPhone },
            success: function (res) {

            },
            error: function () {
                //clearTimeout(timer);
                dialog.toast('获取失败，请检查网络连接后重试', 'none', 1000);
            }
        });
    }
});
//timer处理函数
function SetRemainTime() {
    console.log(timer);
    if (duration == 0) {
        clearTimeout(timer); //停止计时器
        duration = 30;
        $getCapcha.removeClass('btn-disabled')
            .removeAttr('disabled')
            .text("重新发送");

    } else {
        duration--;
        console.log(duration);
        $getCapcha.addClass('btn-disabled')
            .text(duration + 1 + "秒后重试");
        timer = setTimeout(arguments.callee, 1000)

    }
}